const openid = getApp().globalData.openid;

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        title: {
            type: String,
            value: ""
        },
        index: {
            type: Number
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        imgSrc: "/icon/default.png",
        name: "",
        display: "none",
        cloudImgSrc: ""
    },

    /**
     * 组件的方法列表
     */
    methods: {
        //添加图片
        chooseImage(e) {
            var that = this;
            wx.chooseImage({
                count: 1,
                sizeType: ["compressed"],
                sourceType: ["album", "camera"],
                success: function (res) {
                    const tempFilePaths = res.tempFilePaths;
                    that.setData({
                        imgSrc: tempFilePaths[0]
                    });
                }
            })
        },

        //获取input值
        onInput(e) {
            this.setData({
                name: e.detail.value
            })
        },

        //确认添加
        submit(e) {
            //获取输入的菜名和图片
            const name = this.data.name,
                imgUrl = this.data.imgSrc,
                title = this.data.title,
                index = this.data.index;
            const memoObj = name;
            const listObj = {name, imgUrl, fileId: ""}

            //如果输入为空返回父组件警告提醒
            if (name == "") {
                this.triggerEvent("warn", true);
            } else {
                //添加到备忘录缓存中
                var memo, key, value;

                if (title == "添加自定义买菜种类" || title == "添加自定义菜单种类") {
                    value = memoObj;this.triggerEvent("updateList", value);
                } 
                
                else if (title == "添加自定义买菜项") {
                    value = listObj;
                    //图片上传到云储存中
                    this.setData({
                        cloudImgSrc: "userImages/" + openid + "/buyListSrc/" + imgUrl.split("/")[3]
                    });
                    wx.cloud.uploadFile({
                        cloudPath: this.data.cloudImgSrc,
                        filePath: imgUrl,
                        success: res => {
                            console.log("图片上传成功:", res);
                            value.fileId = res.fileID;
                            this.triggerEvent("updateList", value);
                        },
                        fail: err => {
                            console.log(err);
                            wx.showToast({
                                title: '图片上传失败',
                            });
                        }
                    });
                } 
                
                else if (title == "添加自定义菜单项") {
                    value = listObj;
                    //图片上传到云储存中
                    this.setData({
                        cloudImgSrc: "userImages/" + openid + "/menuListSrc/" + imgUrl.split("/")[3]
                    });
                    wx.cloud.uploadFile({
                        cloudPath: this.data.cloudImgSrc,
                        filePath: imgUrl,
                        success: res => {
                            console.log("图片上传成功:", res);
                            value.fileId = res.fileID;
                            this.triggerEvent("updateList", value);
                        },
                        fail: err => {
                            console.log(err);
                            wx.showToast({
                                title: '图片上传失败',
                            });
                        }
                    });
                }

                //显示添加成功提醒
                wx.showToast({
                  title: '添加成功',
                  duration: 2000
                })

                this.triggerEvent("close", true);

                //重置弹窗数据
                var that = this
                setTimeout(function () {
                    that.setData({
                        name: "",
                        imgSrc: "/icon/default.png",
                    });
                }, 2000);
            }
        }
    }
})